package com.emeraldparser.util;

import java.util.Set;

import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;

public class DeterministicAutomatonBuilder<V, E> extends AbstractDeterministicAutomatonBuilder<V, E> {

	private final Automaton<V, E> nonDeterministicAutomaton;

	public DeterministicAutomatonBuilder(Automaton<V, E> nonDeterministicAutomaton) {
		this.nonDeterministicAutomaton = Preconditions.checkNotNull(nonDeterministicAutomaton, "Non-deterministic automaton must not be null");
	}

	@Override
	protected Iterable<? extends V> getStartVertices() {
		return nonDeterministicAutomaton.getStartVertices();
	}

	/**
	 * Returns the vertices that may be immediately reached from the given vertex without
	 * consuming any edge labels.
	 * 
	 * @param vertex
	 * @return
	 */
	protected Iterable<? extends V> getPeers(V vertex) {
		return nonDeterministicAutomaton.successors(vertex, null);
	}

	protected Iterable<? extends V> getSuccessors(V vertex, E edgeLabel) {
		return nonDeterministicAutomaton.successors(vertex, edgeLabel);
	}

	@Override
	protected Iterable<? extends E> getOutboundEdgeLabels(Iterable<? extends V> closure) {
		Set<E> outboundEdgeLabels = Sets.newHashSet();
		
		for (V vertex : closure) {
			outboundEdgeLabels.addAll(nonDeterministicAutomaton.outboundEdgeLabels(vertex));
		}
		
		return outboundEdgeLabels;
	}

}
